package com.vancouver.knows.business.service.impl;

import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pangu.util.StringUtil;
import com.vancouver.knows.business.BUtils;
import com.vancouver.knows.business.bean.DiscountCategoryInfo;
import com.vancouver.knows.business.bean.HomeGridInfo;
import com.vancouver.knows.business.bean.HomeLiveFeedInfo;
import com.vancouver.knows.business.bean.HomeMenuInfo;
import com.vancouver.knows.business.bean.HomePageCarouseInfo;
import com.vancouver.knows.business.bean.HomePageInfo;
import com.vancouver.knows.business.bean.WeatherInfo;
import com.vancouver.knows.business.cache.PersistentCacheManager;

/**
 * 首页列表解析
 * Created by llf
 */
public class HomePageHandle extends ACheckableJsonParser {

	public static final String HOMEPAGE_CAROUS = "homepage_carous";
	public static final String HOMEPAGE_BREAK_NEWS= "homepage_break_news";
	public static final String HOMEPAGE_MAP = "homepage_map";
	public static final String HOMEPAGE_POSTS_SIZE = "homepage_posts_size";
	public static final String HOMEPAGE_MENU = "homepage_menu";
	public static final String HOMEPAGE_GRID= "homepage_grid";
	public static final String HOMEPAGE_LIVE_FEED= "homepage_live_feed";
	public static final String HOMEPAGE_BUSINESS_CATEGORIES= "homepage_business_categories";
	public static final String HOMEPAGE_WEATHER = "homepage_weather";
	
	private HashMap<String, Object> resultMap = new HashMap<String, Object>();

	@Override
	protected void parserContent(JSONObject content) throws Exception {

		//==========轮播图=================
		try {
			String carouseStr = content.getString("head_posts");
			List<HomePageCarouseInfo> homePageCarousLst = JSON.parseArray(carouseStr, HomePageCarouseInfo.class);
			resultMap.put(HOMEPAGE_CAROUS, homePageCarousLst);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//=======休闲娱乐==================
		try {
			String categories = content.getString("business_categories");
			if (StringUtil.isEmptyString(categories)) {
				resultMap.put(HOMEPAGE_BUSINESS_CATEGORIES, new ArrayList<DiscountCategoryInfo>());
			} else {
				List<DiscountCategoryInfo> businessCategoryInfoList = JSON.parseArray(categories,
						DiscountCategoryInfo.class);	
				Collections.sort(businessCategoryInfoList, new BUtils.DiscountCategoryInfosSorter());
				resultMap.put(HOMEPAGE_BUSINESS_CATEGORIES, businessCategoryInfoList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		//============ Home Menu ======================
		try {
			String homeMenu = content.getString("home_menu");
			if (StringUtil.isEmptyString(homeMenu)) {
				resultMap.put(HOMEPAGE_MENU, new ArrayList<HomeMenuInfo>());
			} else {
				List<HomeMenuInfo> homeMenuInfoList = JSON.parseArray(homeMenu, HomeMenuInfo.class);
				resultMap.put(HOMEPAGE_MENU, homeMenuInfoList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		//==================== Weather =============================
		try {
			String currentWeather = content.getString("current_weather");
			if (!StringUtil.isEmptyString(currentWeather)) {

				WeatherInfo weatherInfo = JSON.parseObject(currentWeather, WeatherInfo.class);
				resultMap.put(HOMEPAGE_WEATHER, weatherInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}


		//============首页9宫格数据======================
		try {
			String homeGrid = content.getString("home_grid");	
			if (StringUtil.isEmptyString(homeGrid)) {
				resultMap.put(HOMEPAGE_GRID, new ArrayList<HomeGridInfo>());
			}else {
				List<HomeGridInfo> homeGridInfo = JSON.parseArray(homeGrid,
						HomeGridInfo.class);	
				resultMap.put(HOMEPAGE_GRID, homeGridInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		//============跑马灯数据======================
		try {
			String liveFeed = content.getString("live_feed");
			if (StringUtil.isEmptyString(liveFeed)) {
				resultMap.put(HOMEPAGE_LIVE_FEED, new ArrayList<HomeGridInfo>());
			}else {
				List<HomeLiveFeedInfo> homeLiveFeedInfo = JSON.parseArray(liveFeed,
						HomeLiveFeedInfo.class);
				resultMap.put(HOMEPAGE_LIVE_FEED, homeLiveFeedInfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//====================首页数据部分=============================
		try {
			
			String contentPostsCount = content.getString("content_posts_count");
			resultMap.put(HOMEPAGE_POSTS_SIZE, Integer.valueOf(contentPostsCount));
			JSONArray postsArray = content.getJSONArray("content_posts");
			List<String> breakNewsLst = new ArrayList<String>();
			List<HomePageInfo> homePageLst = new ArrayList<HomePageInfo>();
			
			for (int i = 0; i < postsArray.size(); i++){
				String postBadge = postsArray.getJSONObject(i).getString("post_badge");
				String postType = postsArray.getJSONObject(i).getString("post_type");
				if (StringUtil.isEmptyString(postBadge) && "breaking_news".equals(postType)){
					String postContent = postsArray.getJSONObject(i).getString("post_content");
					String postId = postsArray.getJSONObject(i).getString("post_id");
					String postTitle = postsArray.getJSONObject(i).getString("post_title");
					String postImage = "";
					try {
						postImage = postsArray.getJSONObject(i).getString("post_attachment_image");	
						
					} catch (Exception e) {
						e.printStackTrace();
					}finally {
						if (StringUtil.isEmptyString(postImage)) {
							postImage = "";
						}
						breakNewsLst.add(postImage);
					}
					breakNewsLst.add(postId);
					breakNewsLst.add(postTitle);
					breakNewsLst.add(postContent);
					breakNewsLst.add(postBadge);
					breakNewsLst.add(postType);
					
				}else{
					HomePageInfo info = new HomePageInfo();
					info.setId(postsArray.getJSONObject(i).getString("post_id"));
					info.setPostsTitle(postsArray.getJSONObject(i).getString("post_title"));
					info.setPostType(postsArray.getJSONObject(i).getString("post_type"));
					info.setPostContent(postsArray.getJSONObject(i).getString("post_content"));
					info.setPostBadge(postsArray.getJSONObject(i).getString("post_badge"));
					info.setRedirType(postsArray.getJSONObject(i).getString("redir_type"));
					info.setRedirPostId(postsArray.getJSONObject(i).getString("redir_post_id"));
					info.setRedirUrl(postsArray.getJSONObject(i).getString("redir_url"));
					info.setRedirBusinessCategory(postsArray.getJSONObject(i).getString("redir_business_category"));
					info.setRedirBusinessSubcategory(postsArray.getJSONObject(i).getString("redir_business_subcategory"));
					info.setPdg(postsArray.getJSONObject(i).getString("post_date_gmt"));
					try {
						String postImage = postsArray.getJSONObject(i).getString("post_attachment_image");	
						info.setPostAttachmentImage(postImage);
						String businessName = postsArray.getJSONObject(i).getString("business_name");
						info.setBusinessName(businessName);
						String postAttachmentImagesString = postsArray.getJSONObject(i).getString("post_attachment_array");
						if (!StringUtil.isEmptyString(postAttachmentImagesString)) {
							info.setPostAttachmentArray(JSON.parseArray(postAttachmentImagesString, String.class));
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
					homePageLst.add(info);
				}
			}
			
			resultMap.put(HOMEPAGE_BREAK_NEWS, breakNewsLst);
			resultMap.put(HOMEPAGE_MAP, homePageLst);

			PersistentCacheManager.getInstance().putObject(PersistentCacheManager.HOMEPAGE_CACHE_KEY, resultMap);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public HashMap<String, Object> getResultMap() {
		return resultMap;
	}

}
